$(function(){
	
	var editor;
	
	$('#reservation').daterangepicker({
		locale : {
			format: 'YYYY-MM-DD',
			separator:' 到 ',
			applyLabel : '确定',
			cancelLabel : '取消',
			customRangeLabel : '自定义',
			daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
			monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月',
				'七月', '八月', '九月', '十月', '十一月', '十二月' ],
			firstDay : 1
		},
		ranges: {
			'今天': [moment().startOf('day'), moment()],
			'昨天': [moment().subtract(1,'days').startOf('day'), moment().subtract(1,'days').endOf('day')],
			'本周': [moment().startOf("week").add(1,'days'),moment().endOf("week").add(1,'days')],
			'上周': [moment().subtract(1,'weeks').startOf("week").add(1,'days'),moment().subtract(1,'weeks').endOf("week").endOf("week").add(1,'days')],
			'本月': [moment().startOf("month"),moment().endOf("month")],
			'上个月': [moment().subtract(1,"month").startOf("month"),moment().subtract(1,"month").endOf("month")],
			'最近7天': [moment().subtract(6,'days'), moment()],
			'最近30天': [moment().subtract(29,'days'), moment()]
		},
		applyClass:'btn-primary',
		alwaysShowCalendars: true,
		autoclose:true
	});
	
	//查看
	$('#myTable').on('click', 'a.check', function(){
		var id = $(this).data('id');
		$.ajax({
			url : ctx + '/institution/getById',
			data : {id:id},
			dataType : 'json',
			type : 'post',
			success : function(data){
				if(data.status != "success"){
					toastr.error(data.msg);
				}else{
					$('#checkModal .modal-title').html(data.record.institutionName);
					$('#checkModal .modal-body').html(data.record.institutionDesc);
					$('#checkModal').modal('show');
				}
			},
			error : function(){
				toastr.error("系统错误");
			}
		});
		
	});
	
	
	//多选框
	$(document).on('change', 'input:checkbox.master', function(){
		if($(this).prop('checked')){
			$('input:checkbox.slaver').prop('checked', 'checked');
		}else{
			$('input:checkbox.slaver').prop('checked', '');
		}
	})
	
})



InitiateSimpleDataTable.init();

function retrieveData( sSource, aoData, fnCallback, oSettings ) {  
	
    aoData.push( { "name": "pageNum", "value": (Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength )+1) });
    aoData.push( { "name": "pageSize", "value": oSettings._iDisplayLength });  
    
    var dateRange = $('#reservation').val();
    var searchVal = $('#searchVal').val();
    var enable = $('#enable').val();
    
    if(dateRange && dateRange.length != 0){
    	var minDate = trim(dateRange.split("-")[0]);
        var maxDate = trim(dateRange.split("-")[1]);
        
        aoData.push({ "name": "minDate", "value": minDate });
        aoData.push({ "name": "maxDate", "value": maxDate });
    }
    
    if(searchVal && searchVal.length != 0){
    	aoData.push({ "name": "searchVal", "value": searchVal });
    }
    
    if(enable == 0 || enable == 1)
    	aoData.push({ "name": "enable", "value": enable });
      
    $.ajax( {  
        "url": sSource,  
        "data": aoData,  
        "cache": false,  
        "dataType": 'json', 
        "type": "POST", 
        "success" :function(response) {  
        	fnCallback(response.returnObject);
        }  
    } );  
}  

//新增或编辑
function addOrUpdateRecord(){
	
	$('#addForm').find('textarea[name="institutionDesc"]').val(editor.html());
	
	$.ajax({
		url : ctx + '/institution/addOrUpdateRecord',
		data : $('#addForm').serialize(),
		dataType : 'json',
		type : 'post',
		success : function(data){
			if(data.status != "success"){
				toastr.error(data.msg);
			}else{
				$('#myModel').modal('hide');
				InitiateSimpleDataTable.init();
			}
		},
		error : function(){
			toastr.error("系统错误");
		}
	});
	
	return false;
}

//新增
$(document).on('click', '#add', function(){
	$('#addForm').find('input[type="hidden"], input[type="text"], select, textarea').val('');//每次新增前，清空modal
	editor.html('');
	$('#myModel').modal('show');
});

//编辑
$('#myTable').on('click', 'a.edit', function(){
	var id = $(this).data('id');
	$.ajax({
		url : ctx + '/institution/getById',
		data : {id:id},
		dataType : 'json',
		type : 'post',
		success : function(data){
			if(data.status != "success"){
				toastr.error(data.msg);
			}else{
				$('#myModel input[id="departmentName"]').val(data.record.sysDepartment.fullName);
				$('#myModel input[name="sysDepartment.departmentId"]').val(data.record.sysDepartment.departmentId);
				$('#myModel input[name="institutionName"]').val(data.record.institutionName);
				$('#myModel input[name="institutionId"]').val(data.record.institutionId);
				editor.html(data.record.institutionDesc);
				$('#myModel').modal('show');
			}
		},
		error : function(){
			toastr.error("系统错误");
		}
	});
	
});

//废除
$('#myTable').on('click', 'a.delete', function(){
	var id = $(this).data('id');
	$.ajax({
		url : ctx + '/institution/updateRecord',
		data : {institutionId:id,enable:0},
		dataType : 'json',
		type : 'post',
		success : function(data){
			if(data.status != "success"){
				toastr.error(data.msg);
			}else{
				InitiateSimpleDataTable.init();
			}
		},
		error : function(){
			toastr.error("系统错误");
		}
	});
	
});
//回车搜索
function search(){
	if(event.keyCode==13){
		InitiateSimpleDataTable.init();
	}
}

